/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2013
 */

package com.gxuwz.byssxsys.action;

import static javacommon.util.extjs.Struts2JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.extjs.ListRange;
import cn.org.rapid_framework.page.Page;

import com.gxuwz.byssxsys.model.StuBaseInfo;
import com.gxuwz.byssxsys.service.StuBaseInfoManager;
import com.gxuwz.byssxsys.vo.query.StuBaseInfoQuery;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

public class StuBaseInfoAction extends BaseStruts2Action implements Preparable,
		ModelDriven {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	// forward paths
	protected static final String QUERY_JSP = "/pages/StuBaseInfo/query.jsp";
	protected static final String LIST_JSP = "/pages/StuBaseInfo/list.jsp";
	protected static final String CREATE_JSP = "/pages/StuBaseInfo/create.jsp";
	protected static final String EDIT_JSP = "/pages/StuBaseInfo/edit.jsp";
	protected static final String SHOW_JSP = "/pages/StuBaseInfo/show.jsp";
	// redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/StuBaseInfo/list.do";

	private StuBaseInfoManager stuBaseInfoManager;

	private StuBaseInfo stuBaseInfo;
	java.lang.Integer id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			stuBaseInfo = new StuBaseInfo();
		} else {
			stuBaseInfo = (StuBaseInfo) stuBaseInfoManager.getById(id);
		}
	}

	/** 通过spring自动注入 */
	public void setStuBaseInfoManager(StuBaseInfoManager manager) {
		this.stuBaseInfoManager = manager;
	}

	public Object getModel() {
		return stuBaseInfo;
	}

	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/**
	 * ExtGrid使用 列表
	 * 
	 * @throws IOException
	 */
	public void extlist() throws IOException {
		// PageRequest<Map> pr =
		// ExtJsPageHelper.createPageRequestForExtJs(getRequest(),
		// DEFAULT_SORT_COLUMNS);
		StuBaseInfoQuery pr = newQuery(StuBaseInfoQuery.class,
				DEFAULT_SORT_COLUMNS);
		Page page = stuBaseInfoManager.findPage(pr);

		List<StuBaseInfo> StuBaseInfolist = (List) page.getResult();
		ListRange<StuBaseInfo> resultList = new ListRange<StuBaseInfo>();
		resultList.setList(StuBaseInfolist);
		resultList.setTotalSize(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(resultList);
	}

	/**
	 * extGrid保存
	 * 
	 * @throws IOException
	 */
	public void extsave() throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			stuBaseInfoManager.save(stuBaseInfo);
			result.put("success", true);
			result.put("msg", "添 加 成 功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(result);
	}

	/**
	 * extGrid修改
	 * 
	 * @throws IOException
	 */
	public void extupdate() throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			stuBaseInfoManager.save(stuBaseInfo);
			result.put("success", true);
			result.put("msg", "修 改 成 功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(result);
	}

	/**
	 * extGrid删除
	 * 
	 * @throws IOException
	 */
	public void extdelete() throws IOException {
		String ids = getRequest().getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			for (int i = 0; i < idarray.length; i++) {
				java.lang.Integer id = new java.lang.Integer(
						(String) idarray[i]);
				stuBaseInfoManager.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(result);
	}

}
